#!/usr/bin/perl
#
# Plot the history of a place to PNG on stdout, using gnuplot.
#
use Counter;
use POSIX;

$name = shift;

open(PLOT, ">&STDOUT");
$places = Counter::openfile("places");

$place = $places->getbykey("name", $name) || die "No such place as $name\n";

print PLOT <<EoF;

set xdata time
set timefmt "%Y-%m-%d"
set data style lines
set xlabel "Day"
set ylabel "Registered users"
set key top left
set terminal png
set size 0.5, 0.4
set format x "%Y\\n%m"
set xrange ["2002-01-01":"2004-01-01"]
set xrange [:*]
set yrange [0:*]
plot '-' using 1:2 notitle
EoF
# the double xrange commands is because doing date:* makes the plot command
# fail. This seems perverted.


@hist = FileTable::Log::history("places", $place->key());
for $ev (sort {$$a{date} cmp $$b{date}} @hist) {
    if ($$ev{date} && $$ev{data} =~ /users:(\d+)\n/m) {
	$users = $1;
	$$ev{date} =~ s/ .*//;
	print PLOT "$$ev{date}\t$users\n";
    }
}
# pritn a terminating point with the current value
print PLOT strftime("%Y-%m-%d", gmtime(time())), "\t", $$place{users}, "\n";




